package com.fun_ordering.service.impl;

import com.fun_ordering.service.CommentInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.CommentInfoMapper;
import com.fun_ordering.pojo.CommentInfo;
import com.fun_ordering.pojo.CommentInfoExample;
import com.fun_ordering.pojo.CommentInfoExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 评论信息表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class CommentInfoServiceImpl implements CommentInfoService {

	@Autowired
	private CommentInfoMapper commentInfoMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<CommentInfo> findAll() {
		return commentInfoMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<CommentInfo> page=   (Page<CommentInfo>) commentInfoMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(CommentInfo commentInfo) {
		commentInfoMapper.insert(commentInfo);		
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(CommentInfo commentInfo){
		commentInfoMapper.updateByPrimaryKey(commentInfo);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param commentId
	 * @return
	 */
	@Override
	public CommentInfo findOne(Integer commentId){
		return commentInfoMapper.selectByPrimaryKey(commentId);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] commentIds) {
		for(Integer commentId:commentIds){
			commentInfoMapper.deleteByPrimaryKey(commentId);
		}		
	}
	
	
		@Override
	public PageResult findPage(CommentInfo commentInfo, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		CommentInfoExample example=new CommentInfoExample();
		Criteria criteria = example.createCriteria();
		
		if(commentInfo!=null){			
						if(commentInfo.getCommentContent()!=null && commentInfo.getCommentContent().length()>0){
				criteria.andCommentContentLike("%"+commentInfo.getCommentContent()+"%");
			}	
		}
		
		Page<CommentInfo> page= (Page<CommentInfo>)commentInfoMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}
	
}
